{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "作业一：\n",
    "复习矩阵相关操作，并完成下题: ",
    "菜价\n",
    "上面的数据，可以在numpy中表示如下 5分\n",
    "X = np.array([[1.2, 1.5, 1.8],\n",
    "[1.3, 1.4, 1.9],\n",
    "[1.1, 1.6, 1.7]])\n",
    "y = np.array([5, 10, 9]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.2 1.5 1.8]\n",
      " [1.3 1.4 1.9]\n",
      " [1.1 1.6 1.7]] [ 5 10  9]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1.2,1.5,1.8],\n",
    "             [1.3,1.4,1.9],\n",
    "             [1.1,1.6,1.7]])\n",
    "y = np.array([5,10,9]).T\n",
    "\n",
    "print(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 使用循环的方式计算每天的采购总金额 ",
    "得到结果为[37.2, 37.6, 36.8]，分别表示7/28、7/29、7/30这三天采购总额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[37.2 37.6 36.8]\n"
     ]
    }
   ],
   "source": [
    "def Sum_money(a,b):\n",
    "    everyday_money = np.multiply(a,b)\n",
    "    threeday_money = everyday_money.sum(axis=1)\n",
    "    print(threeday_money)\n",
    "Sum_money(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 使用矩阵点乘来计算每天的采购总金额（使用np.dot来实现矩阵相乘） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 测试两种方式的性能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[37.2 37.6 36.8]\n",
      "CPU times: user 673 µs, sys: 137 µs, total: 810 µs\n",
      "Wall time: 734 µs\n"
     ]
    }
   ],
   "source": [
    "%time Sum_money(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 32 µs, sys: 7 µs, total: 39 µs\n",
      "Wall time: 43.2 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time np.dot(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.nod效率远远高于循环"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
